package com.yogee.topseller.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.login.model.bean.InterestLabelBean;
import com.yogee.topseller.adapter.InterestLabelAdapter;
import com.yogee.topseller.bean.InterlligentFilterBean;
import com.yogee.topseller.bean.ResultBean;
import com.yogee.topseller.bean.SearchKeyBean;
import com.yogee.topseller.bean.SearchPageBean;
import com.yogee.topseller.bean.SearchsBean;
import com.yogee.topseller.fragment.product.model.bean.ProductBean;
import com.yogee.topseller.fragment.product.model.bean.ProductListBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.view.FlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 搜索
 *
 * @auathor ${XiaoJianghong}
 * @date 2017/7/16.
 */

public class SearchAndSelectActivity extends HttpActivity implements TextView.OnEditorActionListener{
    @BindView(R.id.search_et)
    EditText searchEt;
    @BindView(R.id.product_list)
    LinearLayout productList;

    private String selectProductId;
    private List<SelectProduct> productBeanList = new ArrayList<>();
    private int selectNum = 0;//已选产品数量

    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        switch (i) {
            case EditorInfo.IME_ACTION_SEARCH:
                String name = searchEt.getText().toString();

                if (!name.isEmpty()) {
                    searchIndex(name);
                }
                break;
        }
        return false;
    }

    class SelectProduct {
        public boolean isSelect;
        public ProductBean productBean;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_select;
}

    @Override
    protected void initView() {
        searchEt.setOnEditorActionListener(this);

    }


    @Override
    protected void onResume() {
        super.onResume();
//        loadCompareData();
    }
    /**
     * 获取对比产品列表
     */
    private void loadCompareData() {
        productList.removeAllViews();
        HttpManager.getInstance().loadCompareProducts(AppUtil.getUserId(this)).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<ProductListBean>() {
            @Override
            public void onNext(ProductListBean o) {
                if ("1".equals(o.getResult())) {
                    if (null != o.getProductList() && o.getProductList().size() > 0) {
                        addCompareList(o.getProductList());
                    }
                }
            }
        }, this));
    }

    /**
     * 搜索
     * @param name
     */
    private void searchIndex(String name){
        HttpManager.getInstance().searchResult2(AppUtil.getUserId(SearchAndSelectActivity.this), name, "0", "10").compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<SearchsBean>() {
            @Override
            public void onNext(SearchsBean o) {
                loadingFinished();
                if (null != o.getProductList() && o.getProductList().size() > 0){
                    addCompareList(o.getProductList());
                }
            }
        }, this));


    }

    /**
     * 添加布局数据
     */
    private void addCompareList(List<ProductBean> productUnselect) {
        productList.removeAllViews();
        productBeanList.clear();
        for (int i = 0; i < productUnselect.size(); i++) {
            final int tempNum = i;
            SelectProduct selectProduct = new SelectProduct();
            selectProduct.isSelect = false;
            selectProduct.productBean = productUnselect.get(i);
            productBeanList.add(selectProduct);
            View childView = LayoutInflater.from(SearchAndSelectActivity.this).inflate(R.layout.item_search_toselect, null);



            final ImageView isCheckIv = (ImageView) childView.findViewById(R.id.cb_select);
            LinearLayout selectLl = (LinearLayout) childView.findViewById(R.id.select_ll);

            selectLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (productBeanList.get(tempNum).isSelect) {
                        productBeanList.get(tempNum).isSelect = false;
                        isCheckIv.setImageResource(R.drawable.unselect1);
                    } else {
                        productBeanList.get(tempNum).isSelect = true;
                        isCheckIv.setImageResource(R.drawable.select1);
                    }
                }
            });
            TextView nameTv = (TextView) childView.findViewById(R.id.tv_product_name);
            nameTv.setText(productUnselect.get(i).getName());
            TextView item_product_compare_type_tv = (TextView) childView.findViewById(R.id.item_product_compare_type_tv);
            item_product_compare_type_tv.setText(productUnselect.get(i).getProType());

            TextView tv_sy = (TextView) childView.findViewById(R.id.tv_sy);
            tv_sy.setText(productUnselect.get(i).getExpectReturn());

            TextView tv_qx = (TextView) childView.findViewById(R.id.tv_qx);
            tv_qx.setText(productUnselect.get(i).getInvestmentTerm());

            TextView tv_yj = (TextView) childView.findViewById(R.id.tv_yj);
            tv_yj.setText(productUnselect.get(i).getCommission());


            productList.addView(childView);
        }
    }
    /**
     * 组合选中产品id
     *
     * @return
     */
    private String selectProductIdM() {
        int tempSelectNum = 0;
        selectProductId = "";
        for (int i = 0; i < productBeanList.size(); i++) {
            if (productBeanList.get(i).isSelect) {
                tempSelectNum ++;
                if (TextUtils.isEmpty(selectProductId)) {
                    selectProductId = productBeanList.get(i).productBean.getId();
                } else {
                    selectProductId += "," + productBeanList.get(i).productBean.getId();
                }
            }
        }
        selectNum = tempSelectNum;
        return selectProductId;
    }

    @OnClick({R.id.img_return})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_return:
                finish();
                break;
            case R.id.search_cancle_tv:
//                ToastUtils.showToast(SearchAndSelectActivity.this,"确定");
                if (TextUtils.isEmpty(selectProductIdM())) {
                    ToastUtils.showToast(SearchAndSelectActivity.this, "请选择产品");
                    break;
                }else{

                }
                break;
        }
    }



}